/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package w40k.roster2client;

import java.io.PrintWriter;
import w40k.data.*;

/**
 *
 * @author truskov
 */
public class pageGenerator {
    private Armylist internal;
    
    public pageGenerator(Armylist baseData)
    {
        internal = baseData;
    }
    
    public void generatePage(PrintWriter out)
    {
        try 
        {
            out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Army roster</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>" + internal.getArmy() + "</h1>");
            out.println("<form  name=\"change\" action=\"./sheet\" method=\"post\">");
            out.println("<table border=\"1\">");
            for(Slot s:internal.getSlot())
            {
                out.println("<tr>");
                out.print("<td");
                if (s.getState() == ActivityState.ACTIVE && s.getChoice() != null)
                    out.print(" colspan=\"2\"");
                out.println(">");
                out.println("<h2>" + s.getType() + "</h2>");
                if(s.getState() == ActivityState.MUTABLE)
                {
                    out.println("</td>");
                    out.println("<td>");
                    out.println("<button type=\"submit\" name=\""+s.getType()+"\" value=\"addUnit\">+</button>");
                    out.println("</td>");
                }
                if (s.getState() == ActivityState.ACTIVE && s.getChoice() != null)
                {
                    out.println("<select name=\"unit\">");
                    for(w40k.data.Slot.Choice.Item it: s.getChoice().getItem())
                    {
                        out.println("<option value=\""+it.getCaption()+"\">"+it.getCaption()+"</option>");
                    }
                    out.println("</select>");
                    out.println("<button type=\"submit\" name=\""+s.getType()+"\" value=\"confirmUnit\">V</button>");
                    out.println("<button type=\"submit\" name=\""+s.getType()+"\" value=\"cancelUnit\">X</button>");
                    out.println("</td>");
                }
                out.println("<td>");
                if (s.getTotal() > 0)
                    out.println("Total: " + s.getTotal());
                out.println("</td>");
                out.println("</tr>");
                
                if(s.getUnit().size() > 0)
                {
                    out.println("<tr>");
                    out.println("<td colspan=\"2\">");
                    out.println("<table border=\"1\">");
                    for(int un = 0; un < s.getUnit().size(); un++)
                    {
                        Unit cUnit = s.getUnit().get(un);
                        writeUnit(cUnit,out,s.getType()+"_"+un);
                    }
                    out.println("</table>");
                    out.println("</td>");
                    out.println("</tr>");
                }
            }
            out.println("</table>");
            out.println("</form>");
            out.println("<p>Total: " + internal.getTotal() + "</p>");
            if (internal.getValid())
                out.println("<p>Roster is valid</p>");
            out.println("</body>");
            out.println("</html>");
            //
        }
        catch(Exception e)
        {
            e.printStackTrace(out);
        }
        finally 
        {
            out.close();
        }
    }
    private void writeUnit(Unit cUnit, PrintWriter out, String uniqU)
    {
        out.println("<tr>");
        out.println("<td colspan=\"3\"><h3>");
        out.println(cUnit.getType());
        out.println("</h3></td>");
        out.println("<td>");
        if (cUnit.getState() != ActivityState.FROZEN)
            out.println("<button type=\"submit\" name=\""+uniqU+"\" value=\"deleteUnit\">-</button>");
        out.println("</td>");
        out.println("<td>");
        if (cUnit.getTotal() > 0)
            out.println("Total: " + cUnit.getTotal());
        out.println("</td>");
        out.println("</tr>");
        for (int su = 0; su < cUnit.getSubunit().size(); su++)
        {
            Subunit cSub = cUnit.getSubunit().get(su);
            writeSubunit(cSub,out,uniqU+"_" + su);
        }
        out.println("<tr><td colspan=\"4\"><hr/></td></tr>");
    }
    private void writeSubunit(Subunit cSub, PrintWriter out, String uniqSu)
    {
        out.println("<tr>");
        out.println("<td>");
        out.println(cSub.getType());
        out.println("</td>");
        out.println("<td>");
        if (cSub.getState() == ActivityState.ACTIVE)
        {
            //if(cSub.getChoice() == null)
            {
                if (cSub.getQchoice() != null)
                {
                    out.println("<select name=\"qchoice\">");
                    for (int number = cSub.getQchoice().getFrom(); number <= cSub.getQchoice().getTo(); number++)
                    {
                        out.println("<option value=\""+number+"\">"+number+"</option>");
                    }
                    out.println("</select>");
                }
            }
        }
        else
            out.println(cSub.getQuantity());
        out.println("</td>");
        out.println("<td>");
        if (cSub.getState() == ActivityState.MUTABLE)
            out.println("<button type=\"submit\" name=\""+ uniqSu + "\" value=\"subUnitQChange\">.</button>");
        if (cSub.getState() == ActivityState.ACTIVE)
        {
            out.println("<button type=\"submit\" name=\""+ uniqSu + "\" value=\"subUnitQChangeApprove\">V</button>");
            out.println("<button type=\"submit\" name=\""+ uniqSu + "\" value=\"subUnitQChangeDecline\">X</button>");
        }
        out.println("</td>");
        out.println("<td>Cost: "+cSub.getTotal()+"</td>");
        out.println("</tr>");
        for (int ieq = 0; ieq < cSub.getEquipment().size(); ieq++)
        {
            Equipment eq = cSub.getEquipment().get(ieq);                            
            writeEquipment(eq,out, uniqSu+ "_" + ieq);
        }
    }
    private void writeEquipment(Equipment eq, PrintWriter out, String uniqEq)
    {
        if (eq.getType() == null || eq.getType().length() == 0 )
        {
            if (eq.getState() == ActivityState.MUTABLE)
            {
                out.println("<tr>");
                out.println("<td span=\"4\">");
                out.println("<button type=\"submit\" name=\""+uniqEq + "\" value=\"updateStart\">+</button>");                                        
                out.println("</td>");
                out.println("</tr>");
            }
        }
        else
        {
            out.println("<tr>\n<td>*</td>");
            out.println("<td>");
            out.println(eq.getType());
            out.println("</td>");
            out.println("<td>");
            if(eq.getTotal() > 0)
                out.println("(" + eq.getTotal() + ")");
            out.println("</td>");
            out.println("<td>");
            if (eq.getState() == ActivityState.MUTABLE)
                out.println("<button type=\"submit\" name=\""+uniqEq + "\" value=\"updateRevert\">-</button>");                                            
            out.println("</td>");
            out.println("</tr>");
        }
    }
}
